package com.csxh;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

public class PlayingCards {

	public static void main(String[] args) {
		List<Poker> list = shuffle(zuhe());
		List<Poker> list1 = new ArrayList<Poker>();
		List<Poker> list2 = new ArrayList<Poker>();
		List<Poker> list3 = new ArrayList<Poker>();
		Random random = new Random();
		for (int i = 0; i < 17; i++) {
			int index = random.nextInt(list.size());
			Poker poker = list.get(index);
			list1.add(poker);
			list.remove(index);
		}
		list1.sort(new Comparator<Poker>() {
			@Override
			public int compare(Poker o1, Poker o2) {
				if (o1.color == -1 && o2.color == -1) {
					return o1.num - o2.num;
				} else if (o1.color == -1 && o2.color != -1) {
					return 1;
				} else if (o1.color != -1 && o2.color == -1) {
					return -1;
				}
				if (o1.num == o2.num) {
					return o1.color - o2.color;
				}
				return o1.num - o2.num;
			}
		});
		System.out.println("甲："+list1);
		for (int i = 0; i < 17; i++) {
			int index = random.nextInt(list.size());
			Poker poker = list.get(index);
			list2.add(poker);
			list.remove(index);
		}
		list2.sort(new Comparator<Poker>() {
			@Override
			public int compare(Poker o1, Poker o2) {
				if (o1.color == -1 && o2.color == -1) {
					return o1.num - o2.num;
				} else if (o1.color == -1 && o2.color != -1) {
					return 1;
				} else if (o1.color != -1 && o2.color == -1) {
					return -1;
				}
				if (o1.num == o2.num) {
					return o1.color - o2.color;
				}
				return o1.num - o2.num;
			}
		});
		System.out.println("乙："+list2);
		for (int i = 0; i < 17; i++) {
			int index = random.nextInt(list.size());
			Poker poker = list.get(index);
			list3.add(poker);
			list.remove(index);
		}
		list3.sort(new Comparator<Poker>() {
			@Override
			public int compare(Poker o1, Poker o2) {
				if (o1.color == -1 && o2.color == -1) {
					return o1.num - o2.num;
				} else if (o1.color == -1 && o2.color != -1) {
					return 1;
				} else if (o1.color != -1 && o2.color == -1) {
					return -1;
				}
				if (o1.num == o2.num) {
					return o1.color - o2.color;
				}
				return o1.num - o2.num;
			}
		});
		System.out.println("丙："+list3);
		System.out.println("底牌："+list);
		Map<String, List<Poker>> map = new HashMap<String, List<Poker>>();
		map.put("甲", list1);
		map.put("乙", list2);
		map.put("丙", list3);
		out(map);
	}

	private static void out(Map<String, List<Poker>> map) {
		List<Poker> list1 = map.get("甲");
		List<Poker> list2 = map.get("乙");
		List<Poker> list3 = map.get("丙");
		Random random = new Random();
		for (int i = 0; i < 17; i++) {
			int index1 = random.nextInt(list1.size());
			int index2 = random.nextInt(list2.size());
			int index3 = random.nextInt(list3.size());
			Poker p1 = list1.get(index1);
			Poker p2 = list2.get(index2);
			Poker p3 = list3.get(index3);
			System.out.println("甲:"+p1+"\t乙:"+p2+"\t丙:"+p3);
			Set<Poker> set = new TreeSet<Poker>(new Comparator<Poker>() {
				@Override
				public int compare(Poker o1, Poker o2) {
					if (o1.color == -1 && o2.color == -1) {
						return o1.num - o2.num;
					} else if (o1.color == -1 && o2.color != -1) {
						return 1;
					} else if (o1.color != -1 && o2.color == -1) {
						return -1;
					}
					if (o1.num == o2.num) {
						return o1.color - o2.color;
					}
					return o1.num - o2.num;
				}
			});
			set.add(p1);
			set.add(p2);
			set.add(p3);
			list1.remove(index1);
			list2.remove(index2);
			list3.remove(index3);
			System.out.println("顺序为："+set);
		}
	}

	private static List<Poker> shuffle(List<Poker> list) {
		Collections.shuffle(list);
		System.out.println("洗牌后："+list);
		return list;
	}

	private static List<Poker> zuhe() {
		PokerNumber[] pn = PokerNumber.values();
		PokerColor[] pc = PokerColor.values();
		PokerKing[] pk = PokerKing.values();
		List<Poker> list = new ArrayList<Poker>();
		int num;
		int color;
		String name;
		for (int i = 0; i < pn.length; i++) {
			for (int j = 0; j < pc.length; j++) {
				num = pn[i].ordinal();// 返回在枚举中的序号
				color = pc[j].ordinal();
				name = pc[j].getValue() + pn[i].getValue();
				Poker poker = new Poker(name, color, num);
				list.add(poker);
			}
		}
		for (int i = 0; i < pk.length; i++) {
			num = pk[i].ordinal();
			name = pk[i].getValue();
			Poker poker = new Poker(name, num);
			list.add(poker);
		}

		System.out.println("牌："+list);
		return list;
	}
}

class Poker {
	String name;
	int color;
	int num;

	public Poker() {
		super();
	}

	public Poker(String name, int color, int num) {
		super();
		this.name = name;
		this.color = color;
		this.num = num;
	}

	public Poker(String name, int num) {
		super();
		this.name = name;
		this.num = num;
		this.color = -1;
	}

	@Override
	public String toString() {
		return name;
	}

}